﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.FileProviders;
using Org.BouncyCastle.Bcpg.OpenPgp;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace EasyClassWEBAPI.Utils
{
    /// <summary>
    /// 文件存放文件夹的路径管理
    /// 文件主要有两大类：每一个学习小组有一个文件列表，每一个post有一个附件资源列表
    /// </summary>
    public class PathManager
    {
        //所有文件存放的根目录
        public static string DataPath = @"AppData\";
        /// <summary>
        /// 获取文件夹存放的路径
        /// Example：RootPath\AppData\Group\1\
        /// </summary>
        /// <param name="ContentRootPath"></param>
        /// <param name="ID"></param>
        /// <param name="isGroup"></param>
        /// <returns></returns>
        public static string GetPath(string ContentRootPath,int ID,bool isGroup)
        {
            string curPath;
            if (isGroup) 
                curPath = DataPath + @"StudyGroup\";
            else
                curPath = DataPath + @"Post\";
            curPath += @$"{ID}\";
            string resourcePath = Path.Combine(ContentRootPath, curPath);
            //如果文件夹不存在则创建文件夹
            if(!Directory.Exists(resourcePath))
            {
                Directory.CreateDirectory(resourcePath);
            }    
            return resourcePath;
        }

        /// <summary>
        /// 获取Post存放的位置
        /// </summary>
        /// <param name="ContentRootPath"></param>
        /// <param name="PostID"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static string GetPostPath(string ContentRootPath,int PostID,string filePath)
        {
            string PostPath = ContentRootPath + @"\" + DataPath + @"Post\" + @$"{PostID}\{filePath}";
            return PostPath;
        }
        /// <summary>
        /// 正则表达式匹配Post的文件名
        /// </summary>
        /// <param name="name"></param>
        /// <returns></returns>
        public static bool isContainPost(string name)
        {
            //匹配Post的名字
            string RegexString = @"\d{4}-\d{2}-\d{2}--\d{2}-\d{2}-\d{2}.md";
            return Regex.IsMatch(name, RegexString);
        }
    }
}
